/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package app.model_package;

import java.util.ArrayList;
import java.util.Date;

/**
 * 
 * @author haythem
 */
public class EventModel {

	// the ID of the event
	private String eventID;

	// the ID of the task
	private String task;
	// list of agents performing the action
	private String iniator;

	// list of agent receiving a directive
	private ArrayList<String> listReceivers;

	// performative associated to the task
	private String perfomative;

	// the date of the task
	private Date date;

	// attribute conversation
	private String conversationID;

	// /////////////////////////////////////////////////////

	public EventModel(String eventID, String task, String iniator,
			ArrayList<String> listReceivers, String perfomative,
			String conversation, Date date) {
		this.eventID = eventID;
		this.task = task;
		this.iniator = iniator;
		this.listReceivers = listReceivers;
		this.perfomative = perfomative;
		this.date = date;
		this.conversationID = conversation;
	}

	// ///////////////////////////////////////////////////////

	public String toString() {
		String s = "    <event> \n";
		s += "       <task name='" + this.task + "'/> \n";
		s += "       <performatif name='" + this.perfomative + "' /> \n";
		s += "       <actor name='" + this.iniator + "'/> \n";
		s += "       <receivers> \n";
		for (int i = 0; i < listReceivers.size(); i++) {
			s += "           <receiver name='" + listReceivers.get(i)
					+ "'/> \n";
		}
		s += "       </receivers> \n";
		s += "       <conversation id='" + this.conversationID + "' /> \n";
		s += "       <timestemp date='" + this.date.toString() + "' /> \n";
		s += "    </event> \n";
		return s;
	}

	// /////////////////////////////////////////////////////

	public Date getDate() {
		return date;
	}

	public String getEventID() {
		return eventID;
	}

	public String getIniator() {
		return iniator;
	}

	public ArrayList<String> getListReceivers() {
		return listReceivers;
	}

	public String getPerfomative() {
		return perfomative;
	}

	public String getTask() {
		return task;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setEventID(String eventID) {
		this.eventID = eventID;
	}

	public void setIniator(String iniator) {
		this.iniator = iniator;
	}

	public void setListReceivers(ArrayList<String> listReceivers) {
		this.listReceivers = listReceivers;
	}

	public void setPerfomative(String perfomative) {
		this.perfomative = perfomative;
	}

	public void setTask(String task) {
		this.task = task;
	}

	/**
	 * 
	 * @param previousTaskID
	 * @param newtaskID
	 */
	public void modifTask(String previousTaskID, String newtaskID) {
		if (this.task.equals((String) previousTaskID)) {
			setTask(newtaskID);
		} else {
			System.out.println(previousTaskID + " doesn't exist as a Task");
		}
	}

	/**
	 * 
	 * @param previousIniatorID
	 * @param newIniatorID
	 */
	public void modifIniator(String previousIniatorID, String newIniatorID) {
		if (this.iniator.equals((String) previousIniatorID)) {
			setIniator(newIniatorID);
		} else {
			System.out.println(previousIniatorID
					+ " doesn't exist as a Iniator");
		}
	}

	/**
	 * 
	 * @param previousReceiverID
	 * @param newReceiverID
	 */
	public void modifReceiver(String previousReceiverID, String newReceiverID) {
		int i = listReceivers.indexOf((String) previousReceiverID);
		if (i < 0) {
			System.out.println(previousReceiverID
					+ " doesn't exist as a Receiver");
		} else {
			listReceivers.set(i, newReceiverID);
		}
	}

	/**
	 * 
	 * @param ReceiverID
	 */
	public void deleteReceiver(String ReceiverID) {
		listReceivers.remove((String) ReceiverID);
	}

	/**
	 * 
	 * @param ReceiverID
	 */
	public void addReceiver(String ReceiverID) {
		boolean b = listReceivers.contains((String) ReceiverID);
		// we dont allow doubles
		if (b == false) {
			listReceivers.add(ReceiverID);
		}
	}

	/**
	 * 
	 * @param previousPerformativ
	 * @param newPerformativ
	 */
	public void modifPerformativ(String previousPerformativ,
			String newPerformativ) {
		if (this.perfomative.equals((String) previousPerformativ)) {
			setPerfomative(newPerformativ);
		} else {
			System.out.println(previousPerformativ
					+ " doesn't exist as a Performative");
		}
	}

	/**
	 * 
	 * @param previousDate
	 * @param newDate
	 */
	public void modifDate(Date previousDate, Date newDate) {
		if (this.date.equals((Date) previousDate)) {
			setDate(newDate);
		} else {
			System.out.println(previousDate.toGMTString()
					+ " doesn't exist as a Date");
		}
	}

	/**
	 * looks if the some of the agents that operating in this task have as ID
	 * previousAgentID and replace it by newAgentID
	 * 
	 * @param previousAgentID
	 * @param newAgentID
	 */
	public void editAgentInCase(String previousAgentID, String newAgentID) {
		if (this.iniator.equals((String) previousAgentID)) {
			this.setIniator(newAgentID);
		}
		for (int i = 0; i < listReceivers.size(); i++) {
			if (listReceivers.get(i).equals((String) previousAgentID)) {
				listReceivers.set(i, newAgentID);
			}
		}
	}

	/**
	 * this method allows to copy the elements of the event in an other new one
	 * and send it back
	 * 
	 * @return
	 */
	public EventModel copy() {
		return new EventModel(eventID, task, iniator, listReceivers, perfomative,
				conversationID, date);
	}

	public String getConversationID() {
		return conversationID;
	}

	public void setConversationID(String conversationID) {
		this.conversationID = conversationID;
	}

	public void modifConversation(String previousConversation,
			String newConversation) {
		if (this.conversationID.equals((String) previousConversation)) {
			setConversationID(newConversation);
		} else {
			System.out.println(previousConversation
					+ " doesn't exist as a conversation");
		}
	}

}
